﻿using NewRen.Areas.Admin.Schema;
using NewRen.Helpers;
using NewRen.Log4net;
using NewRen.Models;
using NewRen.Models.ViewModel;
using NewRen.Schemas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NewRen.Areas.Admin.Controllers
{
    [LoginCheck]
    public class RecommendController : BaseController
    {
        // GET: UserRecommend
        public ActionResult Index(int p = 0)
        {

            IEnumerable<UserRecommend> query = db.UserRecommends.AsEnumerable();
            //   query = query.OrderByDescending(x => x.AddTime);
            ViewBag.PageInfo = PagerHelper.Do(ref query, 50, p);
            return View(query);
        }

        #region 增加推荐
        /// <summary>
        /// 增加推荐界面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }
        /// <summary>
        /// 增加推荐
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Add(UserRecommend model)
        {
           
            if (ModelState.IsValid)
            {
                try
                {
                    UserRecommend recommend = new UserRecommend();
                    recommend.UserID = model.UserID;
                    db.UserRecommends.Add(recommend);
                    db.SaveChanges();
                    return Redirect("/Admin/Recommend/Index");
                }
                catch(Exception ex)
                {
                    log.Error(new LogContent("增加推荐出错", HttpHelper.GetIPAddress()), ex);
                }
            }
            else
            {
                ModelState.AddModelError("", "推荐信息填写错误！");
            }
            return View();
        }
        #endregion

        #region 修改推荐
        /// <summary>
        /// 修改推荐界面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Edit(int id)
        {
            UserRecommend recommend = new UserRecommend();
            recommend = db.UserRecommends.Find(id);
            ViewBag.Recommend = recommend;
            return View();
        }
        /// <summary>
        /// 修改推荐功能
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(UserRecommend model)
        {
            if (ModelState.IsValid)
            {
                UserRecommend recommend = db.UserRecommends.Find(model.ID);
                recommend.UserID = model.UserID;
                recommend.Priority = model.Priority;
                db.SaveChanges();
                return Redirect("/Admin/Recommend/Index");
            }
            else
            {
                ModelState.AddModelError("", "推荐信息填写错误！");
            }
            return View();
        }
        #endregion

        #region 删除推荐
        /// <summary>
        /// 删除推荐
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateSID]
        public ActionResult Delete(int id)
        {
            try
            {
                var recommend = db.UserRecommends.Find(id);
                db.UserRecommends.Remove(recommend);
                db.SaveChanges();
                return Content("ok");
            }
            catch
            {
                return Content("fail");
            }
        }
        #endregion
    }
}